<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('修改检查模板')" />
    <th:block th:include="include :: jasny-bootstrap-css" />
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-template-edit" th:object="${editInfo}">
            <input name="id" th:field="*{id}" type="hidden">
            <div class="form-group">    
                <label class="col-sm-3 control-label">模板名称：</label>
                <div class="col-sm-8">
                    <input name="templateName" th:field="*{templateName}" class="form-control" type="text">
                </div>
            </div>
            <input name="templatePath" th:field="*{templatePath}" type="hidden">
            <div class="form-group">
                <label class="col-sm-3 control-label">模板地址：</label>
                <div class="col-sm-8 fileinput fileinput-exists input-group" data-provides="fileinput" style="margin-left: 15px;float: left;width: 63%;">
                    <div class="form-control" data-trigger="fileinput">
                        <i class="glyphicon glyphicon-file fileinput-exists"></i>
                        <span class="fileinput-filename">[[${name}]]</span>
                    </div>
                    <span class="input-group-addon btn btn-white btn-file"><span class="fileinput-new">选择文件</span><span class="fileinput-exists">更改</span><input type="file" id="templateUrl"></span>
                    <a href="#" class="input-group-addon btn btn-white fileinput-exists" data-dismiss="fileinput">清除</a>
                </div>
            </div>
        </form>
    </div>
    <th:block th:include="include :: footer" />
    <th:block th:include="include :: jasny-bootstrap-js" />
    <script type="text/javascript">
        var prefix = ctx + "control/template";

        $("#templateUrl").on('change.bs.fileinput', function (e) {
            const file = this.files[0];
            // 处理自己的业务
            if(file){
                var fileType = file.type;
                const docType = fileType.substring(fileType.lastIndexOf('.'));
                console.log(docType);
                //doc和docx文档的后缀 .document   application/msword
                if (!(docType === '.document' || docType === 'application/msword')) {
                    layer.msg("请上传word文档", {icon: 2});
                    return false;
                }
                const formData = new FormData();
                formData.append('file',file);
                $.operate.fileUpload(formData, $('input[name="templatePath"]'));
            }
        });

        $("#form-template-edit").validate({
            focusCleanup: true
        });

        function submitHandler() {
            if ($.validate.form()) {
                const info = $("input[name='templatePath']").val();
                if (info && (info.substring(info.lastIndexOf(".")) === '.doc' || info.substring(info.lastIndexOf(".")) === '.docx')){
                    save(prefix + "/edit", $('#form-template-edit').serialize());
                }else{
                    layer.msg("请上传word文档", {icon: 2});
                }
            }
        }

        function save(url, data) {
            var config = {
                url: url,
                type: "post",
                dataType: "json",
                data: data,
                beforeSend: function (request) {
                    request.setRequestHeader("X-Access-Token", storage.get("token"));
                    $.modal.loading("正在处理中，请稍后...");
                    $.modal.disable();
                },
                success: function(result) {
                    $.modal.close();
                    window.parent.$.modal.msgSuccess(result.message);
                    reload();
                }
            };
            $.ajax(config)
        }

        function reload() {
            var target = $(window.parent.parent.document.getElementsByClassName("William_iframe")[1]);
            var url = target.attr('src');
            target.attr('src', url).ready();
        }
    </script>
</body>
</html>